package com.situ.company.employee.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.employee.service.EmployeeService;
import com.situ.company.util.FmtRequest;

// 接受请求 获得参数 封装对象 调用方法 返回结果
@WebServlet("/employee")
public class EmployeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    doPost(request,response);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    String action = req.getParameter("action");
	    Object res = ""; 
	    switch (action) {
        case "reg":
            res = reg(req);
            break;
        case "login":
            res = login(req);
            break;
        case "page":
            res = page(req);
            break;
        case "add":
            res = add(req);
            break;
        case "del":
            res = del(req);
            break;
        case "resetPass":
            res = resetPass(req);
            break;
        case "setPass":
            res = setPass(req);
            break;
        case "get":
            res = get(req);
            break;
        case "upd":
        case "setDept":
            res = upd(req);
            break;
        case "updDept":
            updDept(req);
            req.getRequestDispatcher("/web/page/employee/updDept.jsp").forward(req,resp);
            return;
        case "delPic":
            res = delPic(req);
            break;

        default:
            break;
        }
	    FmtRequest.write(resp.getWriter(), res);
	}
	
	private Object delPic(HttpServletRequest req) {
	    EmployeeModel model = new EmployeeModel(req.getParameter("code"));
	    return service.delPic(model);
    }

    private String setPass(HttpServletRequest req) {
	    Map<String, String> fields = new HashMap<>();
	    fields.put("code", "code");
	    fields.put("pass", "pass1");
	    EmployeeModel model = FmtRequest.parseModel(req, EmployeeModel.class, fields);
        return service.update(model);
    }

    private String resetPass(HttpServletRequest req) {
	    EmployeeModel model = new EmployeeModel(req.getParameter("code"));
        return service.resetPass(model);
    }

    private String del(HttpServletRequest req) {
	    EmployeeModel model = new EmployeeModel(req.getParameter("code"));
        return service.delete(model);
    }

    private void updDept(HttpServletRequest req) {
	    EmployeeModel model = new EmployeeModel(req.getParameter("code"));
	    EmployeeModel mdb = service.selectModel(model);
        req.setAttribute("mdb", mdb);
    }

    private String upd(HttpServletRequest req) {
	    EmployeeModel model = FmtRequest.parseModel(req, EmployeeModel.class);
        return service.update(model);
    }

    private EmployeeModel get(HttpServletRequest req) {
	    EmployeeModel model = new EmployeeModel(req.getParameter("code"));
        return service.selectModel(model);
    }

    private String add(HttpServletRequest req) {
	    Map<String, String> fields = new HashMap<>();
        fields.put("name", "name");
        fields.put("code", "code");
        fields.put("codeDept", "dept");
	    EmployeeModel model = FmtRequest.parseModel(req, EmployeeModel.class, fields);
        return service.insert(model);
    }

    private EmployeeService service = new EmployeeService();
	
	private Object page(HttpServletRequest req) {
        Map<String, String> fields = new HashMap<>();
        fields.put("name", "name");
        fields.put("code", "code");
//        fields.put("nameDept", "dept");
        EmployeeModel model = FmtRequest.parseModel(req, EmployeeModel.class, fields);
        String pageIndex = req.getParameter("pageIndex");
        String pageLimit = req.getParameter("pageLimit");
        model.setPageIndex(Integer.parseInt(pageIndex));
        model.setPageLimit(Integer.parseInt(pageLimit));
        model.setPageOn(true);
        List<EmployeeModel> list = service.selectList(model);
        int count = service.selectCount(model);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", count);
        return map;
    }

    private String reg(HttpServletRequest req) {
        Map<String, String> fields = new HashMap<>();
        fields.put("code", "code");
        fields.put("pass", "pass");
        fields.put("name", "name");
        EmployeeModel model = FmtRequest.parseModel(req, EmployeeModel.class, fields);
        
        String authcodeP = req.getParameter("authcode");
        HttpSession session = req.getSession();
        Object authCodeS = session.getAttribute("authCode");
        if (!(boolean) authcodeP.equals(authCodeS)) {
            return "authcode";
        }
        return service.insert(model);
    }
    
    private String login(HttpServletRequest req) {
        EmployeeModel model = FmtRequest.parseModel(req, EmployeeModel.class);
        String res = service.login(model);
        if("1".equals(res)) 
            req.getSession().setAttribute("user", service.selectList(model));
        return res;
    }

}
